<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseenter="changeShow" @mouseleave="leaveIndex">
        <h2 class="all">全部商品分类</h2>
        <!-- 加动画 -->
        <transition name="sort">
          <div v-show="show" class="sort">
          <div class="all-sort-list2">
            <div class="item" v-for="(c1, index) in categoryList" :key="c1.categoryId" @click="goSearch">
              <h3 @mouseenter="changeIndex(index)">
                <a :data-categoryName="c1.categoryName" :data-categoryId1="c1.categoryId">{{ c1.categoryName }}</a>
              </h3>
              <div class="item-list clearfix" :style="{ display: currentIndex === index ? 'block' : 'none' }">
                <div class="subitem" v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                  <dl class="fore">
                    <dt>
                      <a :data-categoryName="c2.categoryName" :data-categoryId2="c2.categoryId">{{ c2.categoryName }}</a>
                    </dt>
                    <dd>
                      <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                      <a :data-categoryName="c3.categoryName" :data-categoryId3="c3.categoryId">{{ c3.categoryName }}</a>
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
//底下的这种写法:是将lodash全部API引入,将来项目打包的时候，体积会大一些
// import _ from "lodash";
//引入手段:按需引入
import throttle from "lodash/throttle";
export default {
  name: "TabNav",
  data() {
    return {
      currentIndex:-1,
      show:true
    };
  },
  methods: {
    //鼠标进入的方法/节流
    changeIndex: throttle(function (index) {
      this.currentIndex = index;
    }, 10),
    // 鼠标移出事件
    leaveIndex() {
      this.currentIndex = -1;
      //隐藏商品分类
      //鼠标离开:在search路由下,在修改数据
      if (this.$route.path != "/home") {
        this.show = false;
      }
    },
    //全部商品分类鼠标进入
    changeShow() {
      //鼠标进入:在search路由下,在修改数据
      if (this.$route.path != "/home") {
        this.show = true;
      }
    },
    //精益求精
    //将全部的子节点的事件委派给父节点->事件回调就一个
    goSearch(e) {
      let { categoryname, categoryid1, categoryid2, categoryid3 } =e.target.dataset
      if (categoryname) {
        // 因为只有a标签有categoryName属性，所以点击其他标签时是undefined
        //点击只要是a,就是往search模块跳转
        var locations = {
          name: "search",
          query: { categoryName: categoryname },
        };
        //一级分类的a
        if (categoryid1) {
          locations.query.category1Id = categoryid1;
        } else if (categoryid2) {
          //二级分类的a
          locations.query.category2Id = categoryid2;
        } else {
          //三级分类的a
          locations.query.category3Id = categoryid3;
        }
        //点击商品分类按钮的时候,如果路径当中携带params参数,需要合并携带给search模块
        if (this.$route.params.keyword) {
          locations.params = this.$route.params;
        }
        //目前商品分类这里携带参数只有query参数
        this.$router.push(locations);
      }
    },
  },
  //不管是Home|Search利用的都是全局组件TypeNav,TypeNav在Home路由与Search路由下状态不一样的。
  //VC身上有一个很值钱的东西$route,这玩意可以让你知道当前TypeNav处于那个路由下
  mounted() {
    if(this.$route.path!='/home')this.show=false
  },
  //计算属性
  computed: {
    //数组的写法:目前书写的是大仓库state的K  ...mapState(['home/getcategoryList'])

    ...mapState({
      categoryList: state => state.home.categoryList,
    }),
  },
};
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }

            &.active {
              background: yellowgreen;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          /*温馨提示:豪哥不想利用样式控制二级、三级分类显示与隐藏,下面的代码进行注释*/
          // &:hover {
          //   .item-list {
          //     display: block;
          //   }
          // }

          &:hover {
            background-color: rgba(135, 207, 235, 0.63);
          }
        }
      }
    }

    /*过渡动画:商品分类 进入阶段*/
    .sort-enter {
      height: 0px;
    }

    .sort-enter-active {
      transition: all 0.3s;
    }

    .sort-enter-to {
      height: 461px;
    }
  }
  // 动画样式
  .sort-enter{
    height: 0;
  }
  .sort-enter-to{
    height: 461px;
  }
  .sort-enter-active{
    transition: all .5s linear;
  }
}
</style>
